﻿#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
//常用查找算法
//find

//查找内置数据类型
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//查找容器中是否有5
	vector<int>::iterator it = v.begin();
	it=find(v.begin(), v.end(), 5);
	if (it == v.end())
	{
		cout << "没有5" << endl;
	}
	else
	{
		cout << "找到了" << endl;
		cout << *it << endl;
	}
}
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
	//重载==号，告诉find如何对比person类型
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
			return false;
	}
	int m_Age;
	string m_Name;
};
//查找自定义数据类型
void test02()
{
	Person p1("AA",10);
	Person p2("BB", 15);
	Person p3("CC", 20);
	Person p4("DD", 25);
	vector<Person>v;
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	vector<Person>::iterator it = find(v.begin(), v.end(), p2);
	if (it == v.end())
	{
		cout << "没找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
		cout << "姓名为" << it->m_Name << "年龄为:" << it->m_Age << endl;
	}
}
int main()
{
	test01();
	test02();
}